﻿using System;
using System.IO;
using System.Windows.Forms;

namespace MyVocabulary.Popup
{
    public partial class RenameFolder : Form
    {
        #region Const

        protected static class FormMessages
        {
            public const string InvalidNameErrorMsg = "Please enter a valid name";
            public const string GeneralErrorMsg = "Error renaming folder";
        }

        #endregion

        protected string folderName;

        public DirectoryInfo NewFolderName;
        public event EventHandler OnFolderRenamed;
        
        public RenameFolder(string folderName)
        {
            this.folderName = folderName;
            InitializeComponent();
            tbName.Text = (new DirectoryInfo(folderName)).Name;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {            
            if(tbName.Text.Length == 0)
            {
                lbError.Text = FormMessages.InvalidNameErrorMsg;
                toolTip1.SetToolTip(lbError, FormMessages.InvalidNameErrorMsg);
                return;                
            }

            try
            {
                var dir = new DirectoryInfo(folderName);
                dir.MoveTo(string.Format("{0}\\{1}", folderName.Substring(0, folderName.Length - dir.Name.Length - 1), tbName.Text));
                NewFolderName = dir;

                if (OnFolderRenamed != null)
                    OnFolderRenamed(this, null);
                Close();
            }
            catch (Exception exp)
            {
                lbError.Text = FormMessages.GeneralErrorMsg;
                toolTip1.SetToolTip(lbError,exp.Message);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}